<template>
  <el-dialog title="分配角色" :visible="showAssign" @close="quxiao">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :label="item.id" :key="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div slot="footer" class="dialog-footer">
      <el-button @click="quxiao">取 消</el-button>
      <el-button type="primary" @click="isOK">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRoleList } from "@/api/setting.js";
import { getUserInfotx } from "@/api/user";
import { assignRoles } from "@/api/employees";
export default {
  data() {
    return {
      list: [],
      roleIds: [],
    };
  },
  props: {
    showAssign: {
      type: Boolean,
      default: false,
    },
    userId: {
      type: String,
      default: null,
    },
  },

  methods: {
    //   取消与关闭
    quxiao() {
      this.roleIds = [];
      this.$emit("update:showAssign", false);
    },
    // 获取角色列表
    async getRoleList() {
      let res = await getRoleList();
      this.list = res.rows;
    },
    // 根据id获取员工基本信息
    async getUserInfotx(id) {
      const { roleIds } = await getUserInfotx(id);
      this.roleIds = roleIds;
    },
    //  确定
    async isOK() {
      try {
        await assignRoles({ id: this.userId, roleIds: this.roleIds });
        this.$message.success("分配成功");
        this.$emit("update:showAssign", false);
      } catch (err) {
        console.log(err);
      }
    },
  },
  created() {
    this.getRoleList();
  },
};
</script>

<style>
</style>